# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.exporters import JsonLinesItemExporter
from fang.items import ESFHouseItem, NewHouseItem


# 持久化
class SavePipeline(object):
    def __init__(self):
        self.newhourse_fp = open("newhouse.json", "wb")
        self.esf_fp = open("esf.json", "wb")
        self.newhourse_exporter = JsonLinesItemExporter(self.newhourse_fp, ensure_ascii=False)
        self.esf_exporter = JsonLinesItemExporter(self.esf_fp, ensure_ascii=False)

    def process_item(self, item, spider):
        if isinstance(item, ESFHouseItem):
            self.esf_exporter.export_item(item)
        else:
            self.newhourse_exporter.export_item(item)
        return item

    def close_spider(self, spider):
        self.newhourse_fp.close()
        self.esf_fp.close()


# 清理
class ClearPipeline(object):
    def process_item(self, item, spider):
        pass


# 去重
class DuplicatePipeline(object):
    pass


# 验证
class CheckPipeline(object):
    pass

